<?php

namespace app\admin\controller;

use app\admin\controller\Base;
use think\Request;
use think\Db;
use traits\think\Instance;

class Company extends Base
{
    public $add;
    public $check;
    public $del;
    public $update;
    
    public function __construct()
    {
        parent::__construct();
        $power = $this->checkPower('公司历史');
        if(empty($power)){
            $this->add = 0;
            $this->check =0;
            $this->update =0;
            $this->del =0;
        }else{
            $this->add = $power[0]['padd'];
            $this->check = $power[0]['pcheck'];
            $this->update = $power[0]['pupdate'];
            $this->del = $power[0]['pdel'];
        }
        
    }

    public function index()
    {
        return $this->fetch('list');
    }

    public function newsList($key='')
    {
        //获取每页显示条数
        $limit = Request::instance()->param('limit') ? Request::instance()->param('limit') : 5;
        //获取当前页数
        $page = Request::instance()->param('page') ? Request::instance()->param('page') : 1;
        //计算偏移量
        $offset = ($page - 1) * $limit;

        if($key == ''){
            $list = Db::name('company')->select();
            $count = count($list);
            //查询当前页显示的数据
            $news = Db::name('company')->limit($offset, $limit)->select();
        }else{
            // 如果有 关键字 就使用模糊查询
            $list = Db::name($key['table'])->where("{$key['condition']}", 'like', "%{$key['key']}%")
                                    ->select();
            $count = count($list);
            $news = Db::name($key['table'])->where("{$key['condition']}", 'like', "%{$key['key']}%")
                                    ->limit($offset, $limit)
                                    ->select();
        }
        
        $data = [
            'code' => 0,
            'msg' => '',
            'count' => $count,
            'limit' => $limit,
            'data' => $news,
        ];

        return json($data);
    }

    // 渲染搜索到的页面信息
    public function search($key){
        return $this->newsList($key);
    }

    // 渲染新闻add页面
    public function add()
    {
        $navs = Db::name('nav')->where('sid', 2)->select();
        $data = [
            'nav' => $navs,
        ];
        $this->assign('news', $data);
        return $this->fetch('add');
    }

    // 渲染预览文章
    public function look(){
        return $this->fetch('look');
    }

    // 渲染类型页面
    public function showType(){
        return $this->fetch('type');
    }

    // 获取新闻类型
    public function getType(){
        $type = Db::name('nav')->where('sid', 2)->select();
        $count = count($type);
        $data = [
            'code' => 0,
            'msg' => '',
            'count' => $count,
            'data' => $type,
        ];

        return json($data);
    }

    //添加或更新
    public function insert()
    {
        $data = input('post.data/a');
        $id = isset($data['id']) ? $data['id'] : '';
        $table = input('post.table');
        $data = array_filter($data);
        if (empty($id)) {
            if($this->add != 1){
                return json(['code' => 3, 'msg' => '您没有这权限！']);
            }
            $res = Db::name($table)->insert($data);
            if ($res) {
                return json([
                    'code' => 0,
                    'msg' => '添加成功!',
                ]);
            } else {
                return json([
                    'code' => 1,
                    'msg' => '添加失败!',
                ]);
            }
        } else {
            if($this->update != 1){
                return json(['code' => 3, 'msg' => '您没有这权限！']);
            }

            $res = Db::name($table)->where('id', $id)->update($data);
            if ($res) {
                return json([
                    'code' => 0,
                    'msg' => '修改成功!',
                ]);
            } else {
                return json([
                    'code' => 1,
                    'msg' => '修改失败!',
                ]);
            }
        }
    }

    // 删除
    public function del($mid,$table,$condition)
    {
        if($this->del != 1){
            return json(['code' => 3, 'msg' => '您没有这权限！']);
        }

        $res = Db::name($table)->where($condition, 'in', $mid)->delete();
        if ($res) {
            return json([
                'code' => 0,
                'msg' => '删除成功！',
            ]);
        } else {
            return json(
                [
                    'code' => 1,
                    'msg' => '删除失败！',
                ]
            );
        }
    }

    // 获取表单信息
    public function upload()
    {
        $table = !empty(input('post.table')) ? input('post.table') : 'banner';
        // 获取表单上传文件
        $file = request()->file('file');
        if (!file_exists("/public/uploads/{$table}/")) {
            mkdir("/public/uploads/{$table}/", 0777, true);
        }

        // 移动到框架应用根目录/public/uploads/ 目录下 
        if ($file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . "uploads/{$table}/");
            //原图路径
            $ori_img_path = "uploads/{$table}/" . $info->getSaveName();
            $ori_img_path = str_replace('\\', '/', $ori_img_path);
            // $save_name = $info->getFilename();

            if ($info) { // 成功上传后 获取上传信息
                // $thumb = $this->thumb($ori_img_path, $save_name, $thumb_path="thumb/{$table}/");
                $thumb = '/'.$ori_img_path;
                $data['data']['src'] = $thumb;
                $data['code'] = 0;
                $data['msg'] = '图片上传成功';
                return json($data);
            } else { // 上传失败获取错误信息 
                $data['code'] = 1;
                $data['msg'] = $file->getError();
                return json($data);
            }
        }
    }

    //制作缩略图
    public function thumb($thumb_source, $thumb_name = '', $thumb_path = 'thumb/')
    {
        //路径
        $path = str_replace('\\', '/', $thumb_source);
        $image = \think\Image::open($path);
        $save_path = 'uploads/' . $thumb_path;
        if (!file_exists($save_path)) {
            mkdir($save_path, 0777, true);
        }
        //缩略图的名称
        $thumb_name = $save_path . 'thumb_' . $thumb_name;
        $thumb_name = str_replace('\\', '/', $thumb_name);
        $image->thumb(150, 150)->save($thumb_name);

        return '/'.$thumb_name;
    }
}
